import * as fs from 'fs'
import * as path from 'path'

import { DfaFilter } from './dfaFilter'

// 加载敏感词列表
const wordList = fs
  .readFileSync(path.resolve(__dirname, 'sensitive_words.txt'), 'utf-8')
  .split(/\r?\n/)
  .filter((word) => word.trim().length > 0)

// 构建 DFA 过滤器
const filter = new DfaFilter(wordList)

/**
 * 对文本内容进行敏感词检测与替换
 * @param text 原始文本
 * @returns 过滤后的文本、命中词、是否被阻断
 */
export function filterSensitive(text: string): {
  filtered: string
  hits: string[]
  blocked: boolean
} {
  const hits = filter.detect(text)
  const filtered = filter.replace(text)
  const blocked = hits.length > 0
  return { filtered, hits, blocked }
}
